﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class FREWORKReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b; 
        string WBBAUFKRS;
        string WBBAUFLFD;
        string WBBAUFPOS;
        string WBBALIEFSCH;
        string VKAUFNum = "";
        string WBBTAX;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public FREWORKReport()
            : base()
        {
        }
        public FREWORKReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            WBBAUFKRS = dicParamater["WBBAUFKRS"].ToString().Trim();
            WBBAUFLFD = dicParamater["WBBAUFLFD"].ToString().Trim();
            WBBAUFPOS = dicParamater["WBBAUFPOS"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() +"年"+ DateTime.Now.Month.ToString() +"月"+ DateTime.Now.Day.ToString()+"日";
        }
        #region------------------------------Function------------------------ 
        private void FillInWBBST(DataTable dtSource)
        {
            try
            {
                string WBBSTKNR = "";
                string WEI = "";
                string WBBDPART = "";
                int x=0,startx = 10;
                int y = 1;
                int page = 0,runs=0;
                int rowsperpage=26;
                int pagestart = 11;                
                x = startx;
                //pagestart = (page) * rowsperpage + startx;                
                bool flgBreak=true;
                decimal qty = 0;
               #region FillExcel

                foreach (DataRow dwSource in dtSource.Rows)
                {
                            WBBDPART = dwSource["WBBDPART"].ToString().Trim();
                            WBBSTKNR = dwSource["WBBSTKNR"].ToString().Trim();
                            WEI = dwSource["QTY"].ToString();
                            qty = System.Math.Round(System.Convert.ToDecimal(WEI), 1);
                            //subtotalqty += qty;
                            
                            x += 1;
                            if (x - pagestart == 10)
                            {
                                
                                x = pagestart;
                                y += 7;
                                runs++;
                              
                                if (runs == 4)
                                {
                                    runs = 0;
                                    page++;
                                    x = (page) * rowsperpage + startx + 1;
                                    pagestart = x;
                                    y = 1;
                                    //int i = 5;
                                    exAgent.SetCellValue(x - 3, y + 15, WBBDPART);
                                }
                               
                            }
                            if (runs == 0 && flgBreak)
                            {

                                exAgent.SetCellValue(x-3, y + 15, WBBDPART);
                                flgBreak = false;

                            }
                            
                            exAgent.SetCellValue(x, y, WBBSTKNR);
                            exAgent.SetCellValue(x, y + 5, qty);
                            
                        }
                        #endregion
                        
                            
                        
                        flgBreak = true;
                  
                HideNullRows(page); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }
        private void FillInABPOS(DataTable dtSource)
        {
            try
            {
                foreach (DataRow dwSource in dtSource.Rows)
                {
                    CellValue = dwSource["LSNAME1"].ToString().Trim();
                    exAgent.SetCellValue(8, 11, CellValue);
                    exAgent.SetCellValue(34, 11, CellValue);
                    exAgent.SetCellValue(60, 11, CellValue);
                    exAgent.SetCellValue(86, 11, CellValue);
                    CellValue = dwSource["ARTICLE"].ToString().Trim();
                    exAgent.SetCellValue(8, 1, CellValue);
                    exAgent.SetCellValue(34, 1, CellValue);
                    exAgent.SetCellValue(60, 1, CellValue);
                    exAgent.SetCellValue(86, 1, CellValue);
                    CellValue = dwSource["ABKKDAUFTNR"].ToString().Trim();
                    exAgent.SetCellValue(8, 20, CellValue);
                    exAgent.SetCellValue(34, 20, CellValue);
                    exAgent.SetCellValue(60, 20, CellValue);
                    exAgent.SetCellValue(86, 20, CellValue);
                    CellValue = dwSource["ABKKDZEICH"].ToString().Trim();
                    exAgent.SetCellValue(8, 25, CellValue);
                    exAgent.SetCellValue(34, 25, CellValue);
                    exAgent.SetCellValue(60, 25, CellValue);
                    exAgent.SetCellValue(86, 25, CellValue);
                    CellValue = dwSource["ORDERNO"].ToString().Trim();
                    exAgent.SetCellValue(4, 23, CellValue);
                    exAgent.SetCellValue(30, 23, CellValue);
                    exAgent.SetCellValue(56, 23, CellValue);
                    exAgent.SetCellValue(82, 23, CellValue);
                    CellValue = System.DateTime.Today.ToShortDateString();
                    exAgent.SetCellValue(5, 23, CellValue);
                    exAgent.SetCellValue(31, 23, CellValue);
                    exAgent.SetCellValue(57, 23, CellValue);
                    exAgent.SetCellValue(83, 23, CellValue);
                    CellValue = dwSource["COLORDES"].ToString().Trim();
                    if (CellValue =="" )
                    {
                        CellValue = dwSource["ABPBESCHICH"].ToString().Trim();
                        exAgent.SetCellValue(8, 6, CellValue);
                        exAgent.SetCellValue(34, 6, CellValue);
                        exAgent.SetCellValue(60, 6, CellValue);
                        exAgent.SetCellValue(86, 6, CellValue);
                    }
                    CellValue = dwSource["REASON"].ToString().Trim().Replace("       ","");
                    exAgent.SetCellValue(9, 4, CellValue);
                    exAgent.SetCellValue(35, 4, CellValue);
                    exAgent.SetCellValue(61, 4, CellValue);
                    exAgent.SetCellValue(87, 4, CellValue);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInGABPOS(DataTable dtSource)
        {
            try
            {
                foreach (DataRow dwSource in dtSource.Rows)
                {
                    CellValue = dwSource["LSNAME1"].ToString().Trim();
                    exAgent.SetCellValue(8, 7, CellValue);
                    exAgent.SetCellValue(34, 7, CellValue);
                    exAgent.SetCellValue(60, 7, CellValue);
                    exAgent.SetCellValue(86, 7, CellValue);
                    CellValue = dwSource["ARTICLE"].ToString().Trim();
                    exAgent.SetCellValue(8, 1, CellValue);
                    exAgent.SetCellValue(34, 1, CellValue);
                    exAgent.SetCellValue(60, 1, CellValue);
                    exAgent.SetCellValue(86, 1, CellValue);
                    CellValue = dwSource["ABKKDAUFTNR"].ToString().Trim();
                    exAgent.SetCellValue(8, 17, CellValue);
                    exAgent.SetCellValue(34, 17, CellValue);
                    exAgent.SetCellValue(60, 17, CellValue);
                    exAgent.SetCellValue(86, 17, CellValue);
                    CellValue = dwSource["ABKKDZEICH"].ToString().Trim();
                    exAgent.SetCellValue(8, 22, CellValue);
                    exAgent.SetCellValue(34, 22, CellValue);
                    exAgent.SetCellValue(60, 22, CellValue);
                    exAgent.SetCellValue(86, 22, CellValue);
                    CellValue = dwSource["ORDERNO"].ToString().Trim();
                    exAgent.SetCellValue(4, 22, CellValue);
                    exAgent.SetCellValue(30, 22, CellValue);
                    exAgent.SetCellValue(56, 22, CellValue);
                    exAgent.SetCellValue(82, 22, CellValue);
                    CellValue = System.DateTime.Today.ToShortDateString();
                    exAgent.SetCellValue(5, 22, CellValue);
                    exAgent.SetCellValue(31, 22, CellValue);
                    exAgent.SetCellValue(57, 22, CellValue);
                    exAgent.SetCellValue(83, 22, CellValue);
                    CellValue = dwSource["REASON"].ToString().Trim().Replace("       ", "");
                    exAgent.SetCellValue(9, 4, CellValue);
                    exAgent.SetCellValue(35, 4, CellValue);
                    exAgent.SetCellValue(61, 4, CellValue);
                    exAgent.SetCellValue(87, 4, CellValue);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void FillInGWBBST(DataTable dtSource)
        {
            try
            {
                string WBBSTKNR = "";
                string WEI = "";
                string WBBDPART = "";
                string WBBWBMLFDNR = "";
                int x = 0, startx = 10;
                int y = 1;
                int page = 0, runs = 0;
                int rowsperpage = 26;
                int pagestart = 11;
                x = startx;
                //pagestart = (page) * rowsperpage + startx;                
                bool flgBreak = true;
                decimal qty = 0;
                #region FillExcel

                foreach (DataRow dwSource in dtSource.Rows)
                {
                    WBBDPART = dwSource["WBBDPART"].ToString().Trim();
                    WBBSTKNR = dwSource["WBBSTKNR"].ToString().Trim();
                    WBBWBMLFDNR = dwSource["WBBWBMLFDNR"].ToString().Trim();
                    WEI = dwSource["QTY"].ToString();
                    qty = System.Math.Round(System.Convert.ToDecimal(WEI), 1);
                    //subtotalqty += qty;

                    x += 1;
                    if (x - pagestart == 10)
                    {

                        x = pagestart;
                        y += 9;
                        runs++;

                        if (runs == 3)
                        {
                            runs = 0;
                            page++;
                            x = (page) * rowsperpage + startx + 1;
                            pagestart = x;
                            y = 1;
                            //int i = 5;
                            exAgent.SetCellValue(x - 3, y + 11, WBBDPART);
                        }

                    }
                    if (runs == 0 && flgBreak)
                    {

                        exAgent.SetCellValue(x - 3, y + 11, WBBDPART);
                        flgBreak = false;

                    }

                    exAgent.SetCellValue(x, y, WBBSTKNR);
                    exAgent.SetCellValue(x, y + 5, qty);
                    exAgent.SetCellValue(x, y + 7, WBBWBMLFDNR);
                }
                #endregion



                flgBreak = true;

                HideNullRows(page);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void HideNullRows(int pages)
        {
            try
            {
                exAgent.HideRows((pages+1)*26+1, 1, 200, 999);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public void GetFREWORKReport(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "FREWORK";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath =   strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\回修单\" + WBBAUFKRS + WBBAUFLFD + ".xls";
                        sheetName = WBBAUFKRS + WBBAUFLFD + WBBAUFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        FillInABPOS(dsSource.Tables["ABPOS"]);
                        FillInWBBST(dsSource.Tables["WBBST"]);
                        //FillInWBBTAX(dsSource.Tables["WBBTAX"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (PrintType == "DirectPrint")
                {
                    exAgent.PrintOut("Shee1");
                }
                exAgent.Save();
                exAgent.Close();
                if (PrintType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
        }
        public void GetFBURLINGReport(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "FBURLING";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\回修单\" + WBBAUFKRS + WBBAUFLFD + ".xls";
                        sheetName = WBBAUFKRS + WBBAUFLFD + WBBAUFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        FillInABPOS(dsSource.Tables["ABPOS"]);
                        FillInWBBST(dsSource.Tables["WBBST"]);
                        //FillInWBBTAX(dsSource.Tables["WBBTAX"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (PrintType == "DirectPrint")
                {
                    exAgent.PrintOut("Shee1");
                }
                exAgent.Save();
                exAgent.Close();
                if (PrintType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
        }
        public void GetGREWORKReport(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "GREWORK";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\回修单\" + WBBAUFKRS + WBBAUFLFD + ".xls";
                        sheetName = WBBAUFKRS + WBBAUFLFD + WBBAUFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        FillInGABPOS(dsSource.Tables["ABPOS"]);
                        FillInGWBBST(dsSource.Tables["WBBST"]);
                        //FillInWBBTAX(dsSource.Tables["WBBTAX"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (PrintType == "DirectPrint")
                {
                    exAgent.PrintOut("Shee1");
                }
                exAgent.Save();
                exAgent.Close();
                if (PrintType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
        }
        public void GetGBURLINGReport(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "GBURLING";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath = strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    //foreach (DataRow dwWASTAMM in dsSource.Tables["WASTAMM"].Rows)
                    if (true)
                    {
                        //exAgent = CreateNewSheet(exAgent, dwWASTAMM);
                        filename = @"C:\intex.w\Report\回修单\" + WBBAUFKRS + WBBAUFLFD + ".xls";
                        sheetName = WBBAUFKRS + WBBAUFLFD + WBBAUFPOS;
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        FillInGABPOS(dsSource.Tables["ABPOS"]);
                        FillInGWBBST(dsSource.Tables["WBBST"]);
                        //FillInWBBTAX(dsSource.Tables["WBBTAX"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (PrintType == "DirectPrint")
                {
                    exAgent.PrintOut("Shee1");
                }
                exAgent.Save();
                exAgent.Close();
                if (PrintType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
        } 
    }
}
